package com.moli.iov.admin.mq.activemq;


import com.moli.iov.admin.model.dto.WebSocketDto;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;

/**
 * <p>
 *  AcitveMQ消息监听器
 * </p>
 *
 * @Auther: zql
 * @Date: 2018/11/27 09:44
 */
@Component
@Slf4j
public class ObjectMessageListener implements MessageListener {

    @Autowired
    private MessageHandler messageHandler;

    @Override
    @JmsListener(destination = "${activemq.delay.topic}")
    public void onMessage(Message message) {
        try {
            if(message instanceof ObjectMessage){
                //接收消息
                ObjectMessage objectMessage = (ObjectMessage) message;
                WebSocketDto webSocketDto = (WebSocketDto)objectMessage.getObject();
//                logger.info("AcitveMQ消息监听器 接收到的消息：{}", webSocketDto.toString());
                messageHandler.handle(webSocketDto);
            }else{
                log.info("ActiveMQ 收到的消息: {}", message);
            }
        } catch (JMSException e) {
            log.error("AcitveMQ消息监听器 onMessage 处理异常", e);
        }
    }


}
